package com.jml.Lock;


/**
 * @author:jml
 * @date: 2019/5/6 19:05
 * @description：
 */

class HoldLockThread implements Runnable {

    private String lockA;
    private String lockB;

    public HoldLockThread(String lockA, String lockB) {
        this.lockA = lockA;
        this.lockB = lockB;
    }

    @Override
    public void run() {
        synchronized (lockA){
            System.out.println(Thread.currentThread().getName()+"自己持有"+lockA+"尝试获得"+lockB);
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            synchronized (lockB){
                System.out.println(Thread.currentThread().getName()+"自己持有"+lockB+"尝试获得"+lockA);
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }

    }
}

public class 死锁 {

    public static void main(String[] args) {
        HoldLockThread holdLockThreadA = new HoldLockThread("lockA", "lockB");
        HoldLockThread holdLockThreadB = new HoldLockThread("lockB", "lockA");
        new Thread(holdLockThreadA,"AA" ).start();
        new Thread(holdLockThreadB,"BB" ).start();
    }
}
